package com.chixing.service.impl;

import com.alibaba.fastjson.JSON;
import com.chixing.mapper.CarMapper;
import com.chixing.mapper.CustomerMapper;
import com.chixing.mapper.MyOrderMapper;
import com.chixing.mapper.StoreMapper;
import com.chixing.pojo.*;
import com.chixing.server.sender.SenderFirstMessage;
import com.chixing.service.MyOrderService;
import com.chixing.util.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Wesley
 * @version 1.0
 * @date 2021/10/5 13:54
 */
@Service
public class MyOrderServiceImpl implements MyOrderService {
    @Autowired
    private MyOrderMapper myOrderMapper;
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private SenderFirstMessage senderFirstMessage;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public ServerResponse getById(Integer myOrderId) {
        String redisMyOrderKey = "myOrderId_"+myOrderId;
        MyOrder myOrder;
        if(!redisTemplate.hasKey(redisMyOrderKey)) {
            myOrder = myOrderMapper.selectByPrimaryKey(myOrderId);
        }else{
            myOrder = (MyOrder) redisTemplate.opsForValue().get(redisMyOrderKey);
        }
        if(myOrder != null)
            return ServerResponse.getSuccess(myOrder);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getByOrderId(Integer myOrderId) {
        String redisMyOrderKey = "myOrderId_"+myOrderId;
        MyOrder myOrder;
        if(redisTemplate.hasKey(redisMyOrderKey))
            myOrder =  (MyOrder) redisTemplate.opsForValue().get(redisMyOrderKey);
        else
            myOrder = myOrderMapper.selectByPrimaryKey(myOrderId);
        Integer custId = myOrder.getCustId();
        Customer customer = customerMapper.selectByPrimaryKey(custId);
        Integer carId = myOrder.getCarId();
        Car car = carMapper.selectByPrimaryKey(carId);
        Integer storeBorrowId = myOrder.getStoreBorrowId();
        Store storeBorrow = storeMapper.selectByPrimaryKey(storeBorrowId);
        Integer storeReturnId = myOrder.getStoreReturnId();
        Store storeReturn = storeMapper.selectByPrimaryKey(storeReturnId);
        OrderCustomerCarStoreVO orderCustomerCarStore = new OrderCustomerCarStoreVO(myOrder, customer, car, storeBorrow, storeReturn);
        if(orderCustomerCarStore != null)
            return ServerResponse.getSuccess(orderCustomerCarStore);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getCustomerOrder(Integer custId,Integer status,Integer pageNum) {
        List<MyOrderCarVO> myOrderCarVOS=new ArrayList<>();
        MyOrderExample myOrderExample = new MyOrderExample();
        MyOrderExample.Criteria criteria = myOrderExample.createCriteria();

        if(status != null && status != -1)
            criteria.andStatusEqualTo(status);
        if(custId != null)
            criteria.andCustIdEqualTo(custId);
        Page<MyOrderCarVO> page = PageHelper.startPage(pageNum, 4);
        List<MyOrder> myOrders = myOrderMapper.selectByExample(myOrderExample);
        for (MyOrder myOrder:myOrders){
            Car car=carMapper.selectByPrimaryKey(myOrder.getCarId());
            myOrderCarVOS.add(new MyOrderCarVO(myOrder,car));
        }

        PageInfo pageInfo = page.toPageInfo();
        pageInfo.setList(myOrderCarVOS);
        if (myOrderCarVOS.size()!=0)
            return ServerResponse.getSuccess(pageInfo);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse saveMyOrder(MyOrder myOrder) {
        redisTemplate.opsForValue().set("myOrderNum_" + myOrder.getMyOrderId(),"",10,TimeUnit.MINUTES);
        redisTemplate.opsForValue().set("myOrderId_" + myOrder.getMyOrderId(),myOrder);
       /* int rows=myOrderMapper.insert(myOrder);
        if (rows>0){
            MyOrderExample myOrderExample = new MyOrderExample();
            MyOrderExample.Criteria criteria = myOrderExample.createCriteria();
            criteria.andMyOrderNumberEqualTo(myOrder.getMyOrderNumber());
            MyOrder myOrder1 = myOrderMapper.selectByExample(myOrderExample).get(0);

            return ServerResponse.getSuccess(rows);
        }
*/
        return ServerResponse.getSuccess(1);
    }

    @Override
    public ServerResponse getMyOrderStoreCar(Integer myOrderId) {
        MyOrderExample myOrderExample = new MyOrderExample();
        MyOrderExample.Criteria criteria = myOrderExample.createCriteria();
        criteria.andMyOrderIdEqualTo(myOrderId);
        MyOrder myOrder = myOrderMapper.selectByExample(myOrderExample).get(0);
        Store store=storeMapper.selectByPrimaryKey(myOrder.getStoreReturnId());
        Car car=carMapper.selectByPrimaryKey(myOrder.getCarId());
        MyOrderStoreCarVO myOrderStoreCarVO = new MyOrderStoreCarVO(myOrder,store,car);
        if (myOrderStoreCarVO!=null)
            return ServerResponse.getSuccess(myOrderStoreCarVO);
        return ServerResponse.getFail(null);
    }

    @Override
    public MyOrder getByOrderNumber(Long orderNumber) {
       /* MyOrderExample myOrderExample = new MyOrderExample();
        MyOrderExample.Criteria criteria = myOrderExample.createCriteria();
        criteria.andMyOrderNumberEqualTo(orderNumber);
        MyOrder myOrder = myOrderMapper.selectByExample(myOrderExample).get(0);*/
        String redisKey = "myOrderId_" + orderNumber;
        MyOrder myOrder = null;
        if(redisTemplate.hasKey(redisKey)){
            myOrder = (MyOrder) redisTemplate.opsForValue().get(redisKey);
        }

        return myOrder;
    }
    @Override
    public ServerResponse saveMyOrderInformation(MyOrder myOrder) {
        int rows=myOrderMapper.insert(myOrder);
        if (rows>0)
            return ServerResponse.getSuccess(rows);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse updateCustomer(Customer customer) {
        int rows=customerMapper.updateByPrimaryKey(customer);
        if (rows>0)
            return ServerResponse.getSuccess(rows);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse updateOrderStatus4(MyOrder myOrder) {
        myOrder.setStatus(4);
        int update = myOrderMapper.updateByPrimaryKey(myOrder);
        if(update > 0){
            return ServerResponse.updateSuccess(myOrder);
        }
        return ServerResponse.updateFail(null);
    }

    @Override
    public ServerResponse updateOrderStatus2(MyOrder myOrder) {
        myOrder.setStatus(2);
        int update = myOrderMapper.updateByPrimaryKey(myOrder);
        if(update > 0){
            return ServerResponse.updateSuccess(myOrder);
        }
        return ServerResponse.updateFail(null);
    }

    @Override
    public void firstPush(MyOrder myOrder) {
        long timeout = myOrder.getReturnCarTime().getTime() - new Date().getTime();
        // 将数据存入Redis中，并伴有定时
        redisTemplate.opsForValue().set("myOrder_"+myOrder.getMyOrderId(),myOrder,timeout, TimeUnit.MILLISECONDS);
        // 推送信息提醒用户取车
        RequestMessage requestMessage = new RequestMessage();
        requestMessage.setRoom(myOrder.getCustId().toString());
        requestMessage.setUserId("000");
        requestMessage.setType("2");
        requestMessage.setQuestionId("questionId");
        requestMessage.setCreateTime(DateUtil.dateToString(myOrder.getMyOrderTime(),DateUtil.DATE_PATTEN_YMDHMS));
        requestMessage.setContent("订单已经预定成功,请记得于"+ DateUtil.dateToString(myOrder.getGetCarTime(),DateUtil.DATE_PATTEN_YMDHM) + "取车哦。");

        senderFirstMessage.send(JSON.toJSONString(requestMessage));
        // 完成
    }

    @Override
    public Integer getCouponId(Integer myOrderId) {
        MyOrder myOrder = myOrderMapper.selectByPrimaryKey(myOrderId);
        return myOrder.getCouponId();
    }
}
